package com.huangliusong.async;
/**
 * 消息队列监听器
 * 
 * @author huangliusong
 *
 */

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
@Component
public class QueueListener implements ApplicationListener<ContextRefreshedEvent>{
	@Autowired
	private HuangliusongQueue huangliusongQueue;
	@Autowired
	private DeferredResfulHolder deferredResfulHolder;
	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		new Thread(()->{
			while(true) {
				if(StringUtils.isNotBlank(huangliusongQueue.getCompleteOrder())) {
					//如果消息队列中有值，说明可以返回了
					String orderNumber=huangliusongQueue.getCompleteOrder();
					System.err.println("返回订单处理结果："+orderNumber);
					deferredResfulHolder.getMap().get(orderNumber).setResult("订单成功");
					huangliusongQueue.setCompleteOrder(null);
				}else {
					//如果消息队列中没有值 则需要再修改100毫秒
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}).start();
	}
}
